﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using ConfigGen.Core.SettingsLoader;
using ConfigGen.Core.TemplateLoader;
using ConfigGen.Core.TemplateProcessing;
using ConfigGen.Core.TokenHandling;
using ConfigGen.Utilities.Xml;
using Microsoft.Practices.Unity;

namespace ConfigGen.Core
{
    /// <summary>
    /// IoC container for ConfigGen
    /// </summary>
    public class ConfigGenContainer : UnityContainer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigGenContainer"/> class.
        /// </summary>
        public ConfigGenContainer()
        {
            InitialiseContainer(this);
        }

        /// <summary>
        /// Initialises the supplied container.
        /// </summary>
        /// <param name="container">Container to initialise.</param>
        public static void InitialiseContainer(UnityContainer container)
        {
            container.RegisterType<ConfigurationFilesGenerator>();

            container.RegisterType<ISettingsLoader, ExcelSettingsLoader>(".xls");
            container.RegisterType<ISettingsLoader, ExcelSettingsLoader>(".xlsx");
            container.RegisterType<ISettingsLoader, CsvSettingsLoader>(".csv");
            container.RegisterType<ISettingsLoader, XmlSettingsLoader>(".xml");

            container.RegisterType<ISpreadsheetHeaderProcessor, SpreadsheetHeaderProcessor>();
            container.RegisterType<ISpreadsheetDataProcessor, SpreadsheetDataProcessor>();
            container.RegisterType<ICellDataParser, CellDataParser>();
            container.RegisterType<IExcelFileLoader, ExcelFileLoader>();
            container.RegisterType<ISpreadsheetPreferencesLoader, SpreadsheetPreferencesLoader>();

            container.RegisterType<IDefaultSettingsCombiner, DefaultSettingsCombiner>();

            container.RegisterType<ITemplateLoader, FileTemplateLoader>();
            container.RegisterType<ITemplateProcessor, TemplateProcessor>();
            container.RegisterType<ITemplatePreferencesLoader, TemplatePreferencesLoader>();
            container.RegisterType<ITokenLocator, TokenLocator>();
            container.RegisterType<ITokenReplacer, TokenReplacer>();

            container.RegisterType<IXmlStreamFormatter, XmlStreamFormatter>();
            container.RegisterType<IHashAlgorithmFactory, HashAlgorithmFactory>();
            var machineNameCtor = new InjectionConstructor(Environment.MachineName);
            container.RegisterType<IMachineConfigurationSettingCollectionFilter, MachineConfigurationSettingCollectionFilter>(machineNameCtor);

            container.RegisterType<IOutputWriter<FileOutputWriterResults>, FileOutputWriter>();

            container.RegisterType<ISingleFileGenerator, SingleFileGenerator>();
            container.RegisterType<ISingleFileGenerationController, SingleFileGenerationController>();
       
            container.RegisterInstance<IReadOnlyContainer>(new ConfigGenReadOnlyContainer(container));
        }
    }
}
